﻿using System;
using System.Configuration;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using AjaxPro;
using Common;
using HCRS.DBACL;
using HCRS.Errors;
using HCRS.Implementation;

namespace JinJiang.BookingEngine.Web.cn
{
    public partial class Show : Page
    {
        public string domain, domainurl;
        public static string photourl = ConfigurationManager.AppSettings["photourl"];
        public static string ratecode = ConfigurationManager.AppSettings["ratecode"];
        private DataAccessLayer _DataAccessLayer = new DataAccessLayer();
        private QueryXml _QueryXmlClass = new QueryXml();
        private Functions _Functions = new Functions();
        private DataSet myDataSet;
        private HotelClass _HotelClass = new HotelClass();
        //UserClass _UserClass = new UserClass();
        private CommonWeb _CommonClass = new CommonWeb();
        private StringBuilder sbPhoto;
        private StringBuilder sbUrl;

        protected void Page_Load(object sender, EventArgs e)
        {
            Utility.RegisterTypeForAjax(typeof (Show), this);
            if (!IsPostBack)
            {
                try
                {
                    var PID = Request.QueryString["P"] == null ? 1048 : Convert.ToInt32(Request.QueryString["P"]);
                    var room = Request.QueryString["R"] == null ? 1 : Convert.ToInt32(Request.QueryString["R"]);
                    var adults = Request.QueryString["A"] == null ? 1 : Convert.ToInt32(Request.QueryString["children"]);
                    var children = Request.QueryString["children"] == null ? 0 : Convert.ToInt32(Request.QueryString["children"]);
                    var spcode = Request.QueryString["sp"] ?? string.Empty;
                    var Chickindate = (Request.QueryString["I"] == null || Request.QueryString["I"] == "") ? DateTime.Today.ToString("yyyy-MM-dd") : _CommonClass.VerifySQL(Request.QueryString["I"]);
                    var Chickoutdate = (Request.QueryString["O"] == null || Request.QueryString["O"] == "") ? DateTime.Today.AddDays(1).ToString("yyyy-MM-dd") : _CommonClass.VerifySQL(Request.QueryString["O"]);
                    googlejs.Visible = false;

                    ratecode = Request.QueryString["C"];
                    if (DataAcl.Common.checkexpojs(ratecode))
                    {
                        googlejs.Visible = true;
                    }
                    if (string.IsNullOrEmpty(ratecode))
                    {
                        chkall.Checked = true;
                        chkall.Attributes.Add("style", "display:none");
                    }
                    domain = "http://www.jinjianghotels.com";
                    if (Request.UrlReferrer != null)
                    {
                        domain = FGBCommon.WebDomain(false, out domainurl);
                    }
                    bodyBooking.Attributes.Add("onload", "SearchHotel();fSetOutLinkIframe('" + domain + "')");

                    hdfspcode.Value = spcode;
                    txtStartTime.Text = Chickindate;
                    txtDepTime.Text = Chickoutdate;
                    var hotel = _DataAccessLayer.RunSqlStrings("select title,Stars,photourl,lowprice,highprice,Hotel_ID,address1,currency,photourl,phone ,localLongitude,localLatitude,Diamond from [hotel] where pid=" + PID + " ");
                    if (hotel != null && hotel.Length > 0)
                    {
                        lblHotelName.Text = hotel[0];
                        if (hotel[1] != null && hotel[1] != "" && hotel[1] != "0")
                        {
                            lblStar.Text = "<img src='images/" + hotel[1] + "star.gif' />";
                        }
                        else if (hotel[12] != null && hotel[12] != "")
                        {
                            for (var i = 0; i < Convert.ToInt32(hotel[12]); i++)
                            {
                                lblStar.Text += "<img src='images/zuanshi.jpg' />";
                            }
                        }
                        else
                        {
                            lblStar.Text = "<img src='images/value.gif' />";
                        }
                        lblAddress.Text = hotel[6];
                        lblCurrency.Text = hotel[7];
                        lblPrice.Text = hotel[3] + "-" + hotel[4];
                        lblPhone.Text = hotel[9];
                        imgHotel.ImageUrl = photourl + hotel[8];
                        var HID = Convert.ToInt32(hotel[5]);
                        //HotelInfo(PID);
                        //AirPortInfo(PID);
                        txtStartTime.Text = Chickindate;
                        txtDepTime.Text = Chickoutdate;
                        dropRoom.SelectedValue = room.ToString();
                        dropAdult.SelectedValue = adults.ToString();

                        hdfadults.Value = adults.ToString();
                        hdfCheckindate.Value = Chickindate;
                        hdfCheckoutdate.Value = Chickoutdate;
                        hdfchildren.Value = children.ToString();
                        hdfpropertyID.Value = HID.ToString();
                        hdfroom.Value = room.ToString();
                    }
                }
                catch (Exception ex)
                {
                    //错误信息发送Email
                    var message = "错误信息:" + ex.Message + ex.Source + ex.StackTrace + ";引发异常的方法:" + ex.TargetSite + "  错误程序:PageLoad";
                    //HCRS.Errors.WebError.SendErrorEmail(Request.Url.ToString(), message);
                    WebError.SendErrorEmail(Request.Url + ";" + message);
                    Response.Write("您要预订的酒店未能找到.请返回首页在酒店名称中输入该酒店查询");
                }
            }
        }

        /// <summary>
        /// 	获取价格
        /// </summary>
        [AjaxMethod]
        public string CheckRates(string spcode, string Checkindate, string Checkoutdate, int hotelID, int adults, int children, int room, bool allprice, string domainurltemp)
        {
            string errmsg;
            var responsHtml = "";
            var ts = Convert.ToDateTime(Checkoutdate) - Convert.ToDateTime(Checkindate);
            var nights = Convert.ToInt32(ts.TotalDays);
            //bool showGuestInfo = true;
            //string plandesc;
            //string planstatus;
            //string roomdesc;
            //string roomname;
            //string ratedesc;
            //string ratename;
            //string responsexml;
            var roomType = "";

            int propertyID;
            var strHotelid = _DataAccessLayer.RunSqlStrings("select top 1 pid from hotel where hotel_id=" + hotelID + " ");
            propertyID = Convert.ToInt32(strHotelid[0]);

            var myHotelClass = new SearchHotelClass();
            var rateClass = "";
            if (!allprice) //不是所有价格
            {
                if (!myHotelClass.RateCodeAvailable(propertyID, "pid", ratecode, out rateClass))
                {
                    return "没有符合条件的房价信息（错误代码001）";
                }
            }
            else
            {
                myHotelClass.RateCodeAvailable(propertyID, "pid", "", out rateClass);
            }

            if (rateClass == "" || rateClass == null)
            {
                return "没有符合条件的房价信息（错误代码002）";
            }

            //_QueryXmlClass.GetRateMap(roomType, rateClass, Checkindate, propertyID, nights, adults, children, room, 0, out  errmsg, out myDataSet);
            var info = new GetRateInfo();
            info.adults = adults;
            info.checkin = Checkindate;
            info.children = children;
            //info.currencycode = "";
            //info.filter = "0";
            info.iata = CommonFun.IATA();
            info.language = 0;
            info.nights = nights;
            info.pid = propertyID;
            info.ratecodes = rateClass.Replace("|", ",").Split(',');
            //info.roomcodes = roomType.Split(',');
            info.rooms = room;

            myDataSet = OrderClass.getexratemap(info, out errmsg);

            if (errmsg == "Error" || errmsg == "noResponse")
            {
                responsHtml = "没有符合条件的房价信息。";
            }

            else if (errmsg == "noEnoughAvailRoom")
            {
                responsHtml = "没有符合条件的价格信息";
            }
            else if (errmsg == "")
            {
                var _H_ratelist = new H_ratelist();
                var url = "checkrates.aspx?HotelID={0}&RoomCode={1}&RateCode={2}&ChkIN={3}&ChkOUT={4}&Rooms={5}&Adult={6}&Child={7}&sp=" + spcode + "&wd=" + HttpUtility.UrlEncode(domainurltemp);
                responsHtml += _H_ratelist.FIT_GetXMLRate(myDataSet, hotelID, Checkindate, Checkoutdate, nights, room, adults, children, url, 0);
            }
            else
            {
                responsHtml = "暂时无法连接到中央预订系统，请稍候再试。";
            }
            return responsHtml + "<br /><br /><br /><br /><br /><br /><br /><br />";
        }
    }
}